﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace yafc.UI.Components
{
	public partial class MicroBlogTextField : UserControl
	{
		private string text;
		private Brush brush = ThemeManager.GetCurrentTheme().GetBrush("TextBrush");

		[BrowsableAttribute(true)]
		public override string Text
		{
			get { return text; }
			set { text = value; PerformLayout(); }
		}

		[BrowsableAttribute(false)]
		public Brush Brush
		{
			get { return ThemeManager.GetCurrentTheme().GetBrush("TextBrush"); }
		}

		[BrowsableAttribute(false)]
		public override Font Font
		{
			get
			{
				return ThemeManager.GetCurrentTheme().GetFont("TextFont");
			}
		}

		public MicroBlogTextField()
		{
			InitializeComponent();
		}

		private void MicroBlogTextField_Paint(object sender, PaintEventArgs e)
		{
			var bounds = new Rectangle(new Point(Padding.Left, Padding.Top),
				new Size(Size.Width - Padding.Left - Padding.Right,
					Size.Height - Padding.Top - Padding.Bottom));
			var g = e.Graphics;

			g.DrawString(Text, Font, Brush, bounds);
		}

		protected override void OnLayout(LayoutEventArgs e)
		{
			base.OnLayout(e);

			if (AutoSize && Text != null && Text.Length > 0)
			{
				using (var g = CreateGraphics())
				{
					var size = g.MeasureString(Text, Font, Size.Width - Padding.Left - Padding.Right).ToSize();
					size.Width = ClientSize.Width;
					ClientSize = size;
				}
			}
		}

		public override string ToString()
		{
			return Size.ToString() + Text;
		}
	}
}
